<template>
  <el-container>
    <el-main>
      <el-form el-form ref="form" :model="searchList" label-width="80px">
        <el-row>
          <el-col :span="4">
            <el-form-item label="文件标题">
              <el-input
                v-model="searchList.uname"
                @input="searchAllData"
                placeholder="根据标题模糊搜索"
              ></el-input>
            </el-form-item>
          </el-col>
          <!-- 
          <el-col :span="4">
            <el-form-item>
              <el-button type="primary">搜索</el-button>
              <el-button>重置</el-button>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>

      <el-table
        ref="multipleTable"
        :data="searchList.uname ? searchedData : tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55"> </el-table-column> -->

        <el-table-column prop="title" label="文件标题" width="120" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="fileName" label="文件名" width="150" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="remake" label="详细信息" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="150"
          :formatter="formatterTime"
        >
        </el-table-column>
        <el-table-column prop="employeeName" label="创建人" width="120">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleDownload(scope.$index, scope.row)"
              >下载</el-button
            >
            <!--
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            -->
            <el-button
              size="mini"
              type="danger"
              v-has="'ROLE_admin'"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="修改文档" :visible.sync="dialogFormVisible">
        <!-- <el-form :model="form">
          <el-form-item label="文档名称" :label-width="formLabelWidth">
            <el-input
              v-model="form.uname"
              autocomplete="off"
              style="width: 220px"
            ></el-input>
          </el-form-item>
          <el-form-item label="详细描述" :label-width="formLabelWidth">
            <el-input
              v-model="form.remark"
              autocomplete="off"
              style="width: 220px"
            ></el-input>
          </el-form-item>
        </el-form> -->
        <AddFile></AddFile>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </el-main>

    <!-- 页脚分页 -->
    <div class="fenye">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-container>
</template>

<script>
import AddFile from "../components/AddFile.vue";
export default {
  components: { AddFile },
  name: "File",
  data() {
    return {
      allData: [],

      tableData: [],
      searchedData: [],
      page: 1, //页脚显示的当前页
      size: 10, //一页多少条数据
      total: 400, //总条目数
      multipleSelection: [], //当前选中的行

      searchList: {
        //搜索条件的
        uname: "",
        uclass: "",
      },
      dialogFormVisible: false, //弹出的修改表单
      form: {
        name: "",
        remark: "",
      },
      formLabelWidth: "120px", //宽度
    };
  },
  methods: {
    //格式化日期
    formatterTime(row, column) {
      let date = new Date(row.createTime);
      let dateF = this.formatDate(date);
      return dateF;
    },
    formatDate(current_datetime) {
      let formatted_date =
        current_datetime.getFullYear() +
        "-" +
        (current_datetime.getMonth() + 1) +
        "-" +
        current_datetime.getDate() +
        " " +
        current_datetime.getHours() +
        ":" +
        current_datetime.getMinutes() +
        ":" +
        current_datetime.getSeconds();
      return formatted_date;
    },
    searchAllData() {
      if (this.searchList.uname != "") {
        this.searchedData = [];
        var reg = new RegExp(this.searchList.uname);
        for (let i = 0; i < this.allData.length; i++) {
          if (this.allData[i].title.match(reg)) {
            console.log(this.allData[i]);
            this.searchedData.push(this.allData[i]);
          }
        }
      }
    },
    //分页表格数据
    getTableData() {
      //allData为全部数据
      this.tableData = this.allData.slice(
        (this.page - 1) * this.size,
        this.page * this.size
      );
      this.total = this.allData.length; //总条目等于allDaTa长度
    },
    //size改变时的回调，参数为当前的size
    handleSizeChange(val) {
      // console.log("改变每页多少条，当前一页多少条", val);
      this.size = val;
      this.page = 1; //size改变时要把page重置为1，再把参数赋值给size，因为size改变会导致page数量改变
      this.getTableData();
    },
    //page改变时的回调函数，参数为当前页码
    handleCurrentChange(val) {
      // console.log("翻页，当前为第几页", val);
      this.page = val;
      this.getTableData();
    },
    //请求所有的文档信息
    getAllDocument() {
      this.$request.get("document/queryAllAndName").then((res) => {
        this.allData = res.data.obj;
        // console.log(this.allData);
        this.getTableData(); //获取信息后更新表单数据
      });
    },
    reset() {
      this.getAllDocument();
      // this.getTableData();
    },

    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.dialogFormVisible = true;
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$request.get("document/deleteById/" + row.id).then((res) => {
        if (res.data.obj) {
          this.$message("删除成功");
          this.reset();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleDownload(index, row) {
      // console.log(row)
      window.open(window.origin + "/api" + row.filePath);
    },
  },
  mounted() {
    this.getTableData();
    this.getAllDocument();
  },
};
</script>

<style scoped>
.el-header {
  background-color: rgb(42, 53, 66);
  color: #fff;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
.el-col {
  /* background-color: #fff; */
  color: #fff;
}
i {
  font-size: 18px;
}
.fenye {
  position: fixed;
  bottom: 10px;
}
</style>